//
//  TaskView.m
//  MyLittleBrother
//
//  Created by speeda on 17/7/30.
//  Copyright © 2017年 speeda. All rights reserved.
//

#import "TaskView.h"
#import "TaskCell.h"
#import "CoverView.h"

@interface TaskView () <UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *taskTableView;
@property (nonatomic, strong) NSMutableArray *taskArray;
@property (weak, nonatomic) IBOutlet UIButton *receiveButton;

@end

@implementation TaskView

- (void)awakeFromNib {

    [super awakeFromNib];
    
    self.receiveButton.layer.cornerRadius = 15;
    self.receiveButton.layer.borderWidth = 1;
    self.receiveButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
    
    self.taskTableView.estimatedRowHeight = 100;
    self.taskTableView.rowHeight = UITableViewAutomaticDimension;
    self.taskTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.taskTableView.tableFooterView = [[UIView alloc] init];
    self.taskTableView.dataSource = self;
    self.taskTableView.delegate = self;
}

+ (instancetype)viewFormXib {

    return [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil].firstObject;
}

#pragma mark -- 获取任务
- (void)requestGettask {
    
    NSString *userid = [[NSUserDefaults standardUserDefaults] objectForKey:kUserid];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"command"] = @"gettask";
    dict[@"userid"] = userid;
    
    [AFHTTPSTool POSTRequestRequestWebsite:Url AndDictionary:dict success:^(NSURLSessionDataTask *dataTask, id responseObject) {
        
        NSDictionary *dic = responseObject;
        NSString *state = [NSString stringWithFormat:@"%@",dic[@"code"]];
        if ([state isEqualToString:@"000000"]) {
            
            
            NSLog(@"获取任务 = %@",responseObject);
            
            
        } else {
            
            NSString *errorText = [NSString stringWithFormat:@"%@",dic[@"msg"]];
            [SVProgressHUD showInfoWithStatus:errorText];
        }
        
        
    } failure:^(NSURLSessionDataTask *dataTask, NSError *error) {
        
        [SVProgressHUD showErrorWithStatus:error.localizedDescription];
    }];
}

- (IBAction)receiveBtnAction:(id)sender {
    
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [UIView animateWithDuration:0.4 animations:^{
        
        self.frame = CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        for (UIView *view in [UIApplication sharedApplication].keyWindow.subviews) {
        if ([view isKindOfClass:[CoverView class]]) {
            
            [view removeFromSuperview];
        }
      }
    }completion:^(BOOL finished) {
        
        [self removeFromSuperview];
    }];
}


#pragma maek -- UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 6;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellID = @"TaskCell";
    TaskCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"TaskCell" owner:nil options:nil];
        cell = [nibs firstObject];
    }
    if (indexPath.row == 0) {
        
        cell.blueViewWidth.constant = (240 - 110) * 0.5;
        
    } else if(indexPath.row == 1){
    
        cell.blueViewWidth.constant = cell.grayView.frame.size.width;
        cell.receiveButton.backgroundColor = [UIColor blueColor];
        [cell.receiveButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }

    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 60;
}



- (NSMutableArray *)taskArray {

    if (!_taskArray) {
        _taskArray = [NSMutableArray array];
    }
    
    return _taskArray;
}

@end
